package view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextField;

import model.User;
import service.UserServiceImpl;

public class CreateDialog extends JDialog implements ActionListener{
	JButton jbtinsert,jbtcancell;
	JLabel jlbuserid,jlbpassword,jlbusername;
	JTextField jtfuserid,jtfpassword,jtfusername;
	User user;
           CreateDialog(){
        	    
        	   	setTitle("用户服务");
        	   	setSize(530,150);
        	   	setLocationRelativeTo(null);//居中
        	   	setResizable(false);
        	   	setLayout(null);
        	   	jlbuserid=new JLabel("用户账号");
        	   	jlbpassword=new JLabel("用户密码");
        	   	jlbusername=new JLabel("用户姓名");
        	  
        	   	jtfuserid=new JTextField(30);
        	   	jtfpassword=new JTextField(30);
        	   	jtfusername=new JTextField(30);
        	   
        	   	jbtinsert=new JButton("注册");      	
        	   	jbtcancell=new JButton("取消");
        	   	
        	   	jlbpassword.setBounds(185, 20, 65, 30);add(jlbpassword);  
        	   	jlbuserid.setBounds(20, 20, 65, 30);   add(jlbuserid);
        	   	jlbusername.setBounds(350,20,65,30);   add(jlbusername);
        	   	
        	   	jtfuserid.setBounds(75, 20, 100, 30);       add(jtfuserid);
        	   	jtfpassword.setBounds(240, 20	,100, 30);  add(jtfpassword);
        	    jtfusername.setBounds(415,20,100,30);       add(jtfusername);
        	   	
        	   	jbtinsert.setBounds(150,70 , 80, 25);  add(jbtinsert);  jbtinsert.addActionListener(this);        	   
        	   	jbtcancell.setBounds(300,70,80,25);add(jbtcancell);  jbtcancell.addActionListener(this);
        	   
        	   	setVisible(true);
           }
           public void actionPerformed(ActionEvent e){
        	   user=new User();
        	   UserServiceImpl temp=new UserServiceImpl();
        	   if(e.getSource() == jbtinsert){
        		   try {
        			   user.setUserid(Integer.parseInt(jtfuserid.getText()));
        			   user.setPassword(jtfpassword.getText().trim());
        			   user.setUsername(jtfusername.getText().trim());
					temp.CreateUser(user);
				} catch ( Exception e1) {
					e1.printStackTrace();
				}
        	   }
        	

        	   if(e.getSource() == jbtcancell){
        		   dispose();
   	 			try {
   					new LoginDialog();
   				} catch (Exception e1) {
			e1.printStackTrace();
   				}
        	   }
           }
           public static void main(String []args){
        	   new CreateDialog();
           }
}
